package com.xqboss.apps.controller.box;

import cn.hutool.core.bean.BeanUtil;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.box.BoxDto;
import com.xqboss.apps.service.box.BoxService;
import com.xqboss.apps.vo.box.BoxMallVo;
import com.xqboss.apps.vo.box.BoxPayInfoVo;
import com.xqboss.apps.vo.box.BoxPayVo;
import com.xqboss.apps.vo.box.RemainingLimitVo;
import com.xqboss.common.annotation.Anonymous;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Administrator
 */

@Api(tags = {"箱子管理"})
@RestController
@RequestMapping("/apps/box")
public class BoxController extends BaseController {

    @Autowired
    private BoxService boxService;

    @ApiOperation("数藏商城列表")
    @PostMapping("list-page")
//    @Anonymous
    public R<List<BoxPayVo>> boxList(@RequestBody PageDto dto) {
        dto.startPage();
        List<BoxMallVo> list = boxService.selectAppListByDto(dto);
        List<BoxPayVo> vo = BeanUtil.copyToList(list, BoxPayVo.class);
        return R.ok(vo, list);
    }

    @ApiOperation("藏品购买详情页")
    @PostMapping("pay-info")
//    @Anonymous
    public R<BoxPayInfoVo> payInfo(@RequestBody BoxDto dto) {
        return boxService.payInfo(dto);
    }

    @ApiOperation("获取箱子剩余可购买数量")
    @GetMapping("remaining-limit/{boxId}")
    public R<RemainingLimitVo> getUserRemainingLimit(@PathVariable("boxId")
                                         @ApiParam(value = "箱子ID", required = true)
                                         @NotNull(message = "请输入箱子ID")
                                         Long boxId) {
        Long userId = getUserId();
        Integer remainingLimit = boxService.getUserRemainingLimit(userId, boxId);
        RemainingLimitVo vo = new RemainingLimitVo();
        vo.setRemainingLimit(remainingLimit);
        return R.ok(vo);
    }
}
